<?php
/**
 * 描述内容(关于我们,公司简介等)操作类
 * Created		: 2009-12-18
 * Modified		: 2010-10-25
 * @version	: 0.1.0
 * @author		: Joseph Chen (jsph.chen@gmail.com)
 */
class Guestbook
{

	/**
	 * 表名
	 * @var string
	 */
	const TBL	= 'guestbook';
	
	/**
	 * 每页显示几条记录
	 * @var int
	 */
	public static $pageSize = 30;
	
	/**
	 * 分页条代码
	 * @var string
	 */
	public static $pagePanel = '';
	
	/**
	 * 读取指定条件的记录
	 * @param string $id
	 */
	public static function read($id) 
	{
		$where = 'id='.(int)$id;
		$record = Db::getOne(self::TBL, $where);
		return $record;
	}
	
	/**
	 * 留言
	 * @param string $guestname
	 * @param string $email
	 * @param string $telephone
	 * @param string $content
	 * @param string $lang
	 * @param string $sign
	 */
	public static function add($guestname, $email, $telephone, $content, $lang='zh-cn') 
	{
		$data = array(
			'guestname'		=> $guestname,
			'email'			=> $email,
			'telephone'		=> $telephone,
			'content'		=> $content,
			'lang'			=> $lang,
			'reply'			=> '',
			'note'			=> '',
			'create_time'	=> time(),
		);
		$ret = Db::insert($GLOBALS['tbl_pre'].self::TBL, $data);
		return $ret;
	}
	
	/**
	 * 回复/备注
	 * @param int $id
	 * @param string $reply
	 * @param string $note
	 */
	public static function replynote($id, $reply, $note=false, $sendmail=false) 
	{
		$data = array();
		if ($reply !== false) {
			$data['reply'] = $reply;
		}  else {
		}
		if ($note !== false) {
			$data['note'] = $note;
		}
		if ($sendmail) {
			$record = Guestbook::read($id);
			if ($record['email'] && !$record['is_sentemail']) {
				$content = $reply ? $reply : $record['reply'];
				$ret = Guestbook::sendReplyMail($content, $record['email'], $record['guestname']);
				if (!$ret) {
					App::msg($GLOBALS['LSendReplyMailFailure']);
					return false;
				}
				$data['is_sentemail'] = 1;
			}
		}
		$ret = Db::update($GLOBALS['tbl_pre'].self::TBL, $data, 'id='.(int)$id);
		return $ret;
	}
	
	/**
	 * 发送回复内容到客户电子邮件
	 * @param string $reply
	 * @param string $guestEmail
	 */
	public static function sendReplyMail($reply, $guestEmail, $guestName) 
	{
		if (!$guestName) {
			$guestName = $GLOBALS['GuestCommonName'];
		}
		$site = include(APP_ROOT.'Config'.DS.'site.cfg.php');
		include(APP_ROOT.'Config'.DS.'email.cfg.php');
		$search = array(
			'{:ToNickName:}', '{:websiteName:}'
		);
		$replace = array(
			$guestName, $site['siteName']
		);
		$subject = str_replace($search, $replace, $subjectTpl);
		$search = array(
			'{:ToNickName:}', '{:reply:}'
		);
		$replace = array(
			$guestName, nl2br("\n\n".$reply)
		);
		$body = str_replace($search, $replace, $bodyTpl);
		$ret = Mailer::send($subject, $body, $guestEmail, $guestName);
		return $ret;
	}
	
	/**
	 * 删除一条信息
	 * @param int $id
	 */
	public static function delete($id) 
	{
		$ret = Db::delete($GLOBALS['tbl_pre'].self::TBL, 'id='.(int)$id);
		return $ret;
	}
	
	/**
	 * 获取列表
	 * @param int $cid
	 * @param boolean $hasPages
	 * @param int $pageSize
	 */
	public static function getList($hasPages=true, $pageSize=30) 
	{
        empty($pageSize) && $pageSize = self::$pageSize;
        
    	if (empty($_GET['guestname']))
    	{
    		$_whr = array();
    		$params = array();
		} else {
			$_whr = array('guestname like :guestname');
			$params['guestname'] = '%'.$kw.'%';
		}
    	if (empty($_GET['content']))
    	{
    		$_whr = array();
    		$params = array();
		} else {
			$_whr = array('content like :content');
			$params['content'] = '%'.$kw.'%';
		}
		
        //关键字查询
        if ($_whr)
        {
	        $where = join(' and ', $_whr);
        } else {
        	$where = '';
        }
        $rows = Db::getCounts($GLOBALS['tbl_pre'].self::TBL, $where, $params);
        if (!$rows && !is_null(Db::errors())) {
        	App::msg(Db::$errors[2]);
			return false;
        }
		if ($hasPages)
		{
			Page::init($pageSize, $rows);
			Page::setFromTo($pageSize);
			$pageStart = Page::$from;
		} else {
			$hasPages = false;
			$pageStart = 0;
		}
        $list = Db::select(
     					$GLOBALS['tbl_pre'].self::TBL, 
     					$where, 
     					'*', 
     					$params, 
     					'', 
     					$pageStart, 
     					$pageSize
    	);
		//如果列表有分页,则进行分页操作
        if ($hasPages == 1)
		{
			self::$pagePanel = Page::generate($_POST);
		}
        return $list;
	}
}













